package com.quincysx.customaidl.demo1;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.quincysx.customaidl.R;
import com.quincysx.customaidl.bean.Book;

import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private IBookManager mIBookManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bindService();
    }

    public void onClick(View view) {
        if (mIBookManager != null) {
            switch (view.getId()) {
                case R.id.btn_add:
                    int index = new Random().nextInt(100000) + 10000;
                    Book book = new Book();
                    book.setId(index);
                    book.setName("我是随机的书#" + index);
                    book.setPrice((new Random().nextInt(100) / 10d) + 10);
                    try {
                        Log.e("==客户端请求添加书籍==", book.toString());
                        mIBookManager.addBook(book);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    break;
                case R.id.btn_get:
                    try {
                        List<Book> bookList = mIBookManager.getBookList();
                        Log.e("==客户端获取到的书籍==", bookList.toString());
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
            }
        }
    }

    /**
     * 绑定 Service
     */
    private void bindService() {
        Intent intent = new Intent(this, RemoteService.class);
        bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        if (mIBookManager != null) {
            mIBookManager.asBinder().unlinkToDeath(mDeathRecipient, 0);
        }
        unbindService(mServiceConnection);
        super.onDestroy();
    }

    /**
     * Service 连接器
     */
    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mIBookManager = IBookManager.Stub.asInterface(service);
            try {
                service.linkToDeath(mDeathRecipient, 0);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        /**
         * 处理远程服务异常死亡的方式之一，环境是 UI 线程
         */
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mIBookManager = null;
        }
    };

    /**
     * 处理远程服务异常死亡的方式之一，环境是 Binder 线程
     */
    private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {
        @Override
        public void binderDied() {
            mIBookManager = null;
            bindService();
        }
    };
}
